Partial Class Admin_User_Roles
  Inherits System.Web.UI.Page

#Region "MPZ Codes"

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If department_cmb.Items.Count < 1 Then
            Dim zClass As New LookUp.General_Lookup
            department_cmb.Items.Clear()
            zClass.Loader(department_cmb, "DEPARTMENT", "Desc", "ID")
            department_cmb.Items.Insert(0, "")
            zClass.LookUpCode = "USER_ROLES"
            DataGrid.CurrentPageIndex = 0
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If

        Dim Edit_Image As Image = CType(Me.Master.FindControl("U2"), Image)
        Edit_Image.ImageUrl = "~/images/redT.PNG"
        Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("User_Roles_btn"), LinkButton)
        Edit_Button.ForeColor = Drawing.Color.Black
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Users_Panel"), Panel)
        Edit_Panel.Visible = True
        SecurePage()
    End Sub

    Protected Sub department_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles department_cmb.SelectedIndexChanged
        'If department_cmb.SelectedItem.Text = "" Then Exit Sub
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "USER_ROLES"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, department_cmb.SelectedValue)

    End Sub

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        If e.CommandName = "Edit" Then
            Session("role_mode") = e.CommandName
            Session("role_cd") = e.Item.Cells(0).Text
            Role_txt.Text = e.Item.Cells(1).Text
            department_cmb.SelectedItem.Selected = False
            department_cmb.Items.FindByValue(e.Item.Cells(5).Text).Selected = True
            department_cmb.Enabled = False
            OnEditMode()

            'TODO: Fix role deletion
            'TODO: Secure Admin Page
        ElseIf e.CommandName = "Delete" Then
            Dim zClass As New LookUp.General_Lookup
            zClass.LookUpCode = "USER_ROLES"
            DataGrid.CurrentPageIndex = 0

            If zClass.OKtoDELETE(e.Item.Cells(0).Text) = True Then

                zClass.ExecQuery("DELETE FROM role_dtl WHERE role_cd='" & e.Item.Cells(0).Text & "'")
                zClass.ExecQuery("DELETE FROM role_mstr WHERE role_cd='" & e.Item.Cells(0).Text & "'")

                If department_cmb.SelectedValue = "" Then
                    zClass.DataGridFill(DataGrid, Results_lbl)
                Else
                    zClass.DataGridFill(DataGrid, Results_lbl, department_cmb.SelectedValue)
                End If

            End If

        ElseIf e.CommandName = "View" Then
            Session("role_cd") = e.Item.Cells(0).Text
            Session("dept_cd") = e.Item.Cells(5).Text
            Response.Redirect("~/Admin/Module_Access.aspx")

        End If
    End Sub

    Private Sub OnEditMode()
        DataGrid.Enabled = False : Add_btn.Enabled = False : AddEdit_Panel.Visible = True : Role_txt.Focus()
    End Sub

    Private Sub NotOnEditMode()
        DataGrid.Enabled = True : Add_btn.Enabled = True : AddEdit_Panel.Visible = False
        department_cmb.Enabled = True
    End Sub

    Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
        Session("role_mode") = "Add"
        OnEditMode()
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        DataGrid.CurrentPageIndex = e.NewPageIndex
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "USER_ROLES"
        If department_cmb.SelectedValue <> "" Then
            zClass.DataGridFill(DataGrid, Results_lbl, department_cmb.SelectedValue)
        Else
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If

    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO

        If Session("role_mode") = "Edit" Then
            Dim i As String = "UPDATE role_mstr SET " & _
                "role_desc=" & UCase(dm.Q(Trim(Role_txt.Text))) & _
                ", role_last_upd_by='" & Session("UserName") & "', role_last_upd_dt='" & _
                Month(Now) & "/" & Day(Now) & "/" & Year(Now) & "'" & _
                " WHERE role_cd='" & Trim(Session("role_cd").ToString) & "'"
            zClass.ExecQuery("UPDATE role_mstr SET " & _
                "role_desc=" & UCase(dm.Q(Trim(Role_txt.Text))) & _
                ", role_last_upd_by='" & UCase(Session("UserName")) & "', role_last_upd_dt='" & _
                Now & "'" & _
                " WHERE role_cd='" & Trim(Session("role_cd").ToString) & "'")

        ElseIf Session("role_mode") = "Add" Then
            zClass.LookUpCode = "ROLE"
            zClass.ExecQuery("INSERT INTO role_mstr(role_cd, role_desc, dept_cd, " & _
                "role_crea_by, role_crea_dt) " & _
                "VALUES(" & UCase(dm.Q(Trim(zClass.MaxCode()))) & "," & _
                UCase(dm.Q(Trim(Role_txt.Text))) & "," & UCase(dm.Q(Trim(department_cmb.SelectedValue))) & ",'" & _
                UCase(Session("UserName")) & "','" & Now & "')")
        End If

        Session.Remove("role_mode") : Session.Remove("role_cd")
        zClass.LookUpCode = "USER_ROLES"
        If department_cmb.SelectedValue <> "" Then
            zClass.DataGridFill(DataGrid, Results_lbl, department_cmb.SelectedValue)
        Else
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If
        NotOnEditMode()

    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        Session.Remove("role_mode")
        Session.Remove("role_cd")
        NotOnEditMode()

    End Sub
#End Region


End Class